﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxj.Web;
using Hxj.Common;
using RicciWebsite.Model;

namespace RicciWebsite.admin
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            JavaScript.TopLocation();
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnlogin_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;

            if (string.Compare(txtvcode.Text.Trim(), RicciWebsite.Common.GetVerifycodeCookie(), true) != 0)
            {
                txtvcode.Text = string.Empty;

                JavaScript.MsgBox("验证码错误!");
                return;
            }

            base_user userinfo = RicciWebsite.RicciDatabase.Default.From<base_user>().Where(base_user._.username == txtusername.Text.Trim()).ToFirst();

            if (null == userinfo)
            {
                txtvcode.Text = string.Empty;
                JavaScript.MsgBox("用户名或密码错误!");
                return;
            }

            if (string.Compare(Password.Encode(txtpasswd.Text.Trim(), userinfo.salt), userinfo.passwd, true) != 0)
            {
                txtvcode.Text = string.Empty;
                JavaScript.MsgBox("用户名或密码错误!");
                return;
            }

            RicciWebsite.Common.SetUserIDCookie(userinfo.userid);


            Response.Redirect("~/admin/main.aspx");
        }
    }
}
